package net.lb.io.json.serializer;

import java.lang.reflect.Type;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import net.lb.baseutil.collection.ObjectCollect;
import net.lb.baseutil.collection.StringCache;
import net.lb.io.json.field.JsonField;
import net.lb.io.json.util.JsonSerializer;

public class DateSerializer implements JsonSerializer
{
    private static DateFormat          format                      = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private ThreadLocal<Calendar>      toStringCalendarInstance    = new ThreadLocal<Calendar>() {
                                                                       @Override
                                                                       protected Calendar initialValue()
                                                                       {
                                                                           Calendar calendar = Calendar.getInstance();
                                                                           return calendar;
                                                                       }
                                                                   };
    private ThreadLocal<Calendar>      toJsonvalueCalendatInstance = new ThreadLocal<Calendar>() {
                                                                       @Override
                                                                       protected Calendar initialValue()
                                                                       {
                                                                           Calendar calendar = Calendar.getInstance();
                                                                           return calendar;
                                                                       }
                                                                   };
    private ThreadLocal<StringBuilder> builderInstance             = new ThreadLocal<StringBuilder>() {
                                                                       @Override
                                                                       protected StringBuilder initialValue()
                                                                       {
                                                                           StringBuilder builder = new StringBuilder();
                                                                           return builder;
                                                                       }
                                                                   };
    
    public void setJsonFields(JsonField[] jsonFields)
    {
    }
    
    @Override
    public Object transToJsonvalue(Object src, ObjectCollect collect)
    {
        Calendar calendar = toJsonvalueCalendatInstance.get();
        calendar.setTime((Date) src);
        StringBuilder builder = builderInstance.get();
        builder.delete(0, builder.length());
        builder.append(calendar.get(Calendar.YEAR)).append('-').append(calendar.get(Calendar.MONTH)).append('-').append(calendar.get(Calendar.DAY_OF_MONTH)).append(' ')
                .append(calendar.get(Calendar.HOUR_OF_DAY)).append(':').append(Calendar.MINUTE).append(':').append(calendar.get(Calendar.SECOND));
        return builder.toString();
    }
    
    @Override
    public void writeToString(StringCache cache, Object value, ObjectCollect collect)
    {
        Calendar calendar = toStringCalendarInstance.get();
        calendar.setTime((Date) value);
        cache.append('"', 40).append(calendar.get(Calendar.YEAR)).append('-').append(calendar.get(Calendar.MONTH)).append('-').append(calendar.get(Calendar.DAY_OF_MONTH)).append(' ')
                .append(calendar.get(Calendar.HOUR_OF_DAY)).append(':').append(Calendar.MINUTE).append(':').append(calendar.get(Calendar.SECOND)).append('"');
    }
    
    @Override
    public Object toBean(Object value, Type type)
    {
        if (value instanceof String)
        {
            try
            {
                return format.parse((String) value);
            }
            catch (ParseException e)
            {
                return new RuntimeException(e);
            }
        }
        else
        {
            return value;
        }
    }
}
